<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 https://www.meoyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 喵云科技 【https://www.meoyun.com】
// +----------------------------------------------------------------------


namespace app\api\controller;

use think\facade\Db;
use app\common\model\Discuss as DiscussModel;

class Discuss extends BaseApi
{
    //随机获取话题列表
    public function random()
    {
        $list = Db::name('discuss')->orderRaw('rand()')->limit(6)->select()->toArray();
        return $this->data($list);
    }

    //七天内话题列表
    public function getListByTopicId()
    {
        $topicId = input("topic_id");

        $list = DiscussModel::withJoin(['userInfo' => ['nickname', 'avatar']])->whereTime('discuss.create_time', '-168 hours')->where("discuss.topic_id", $topicId)->order("id", "desc")->paginate(10);
        return $this->data($list);
    }

    //用户创建话题
    public function userAdd()
    {
        $params = input("post.");
        $params["user_id"] = $this->getUserId();

        $info = DiscussModel::create($params);

        return $this->data($info);
    }

    public function infoById()
    {
        $id = input("id");

        $info = DiscussModel::withJoin(['userInfo'])->where("discuss.id", $id)->find();

        Db::name('discuss')->where('id', $id)->inc('see_num')->update();

        return $this->data($info);
    }
}